package com.kidback.bbloc259.util;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;

import androidx.annotation.ColorRes;
import androidx.annotation.RawRes;
import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.util.IOUtils;
import com.kidback.bbloc259.app.KidApplication;

import java.io.InputStream;

/**
 * Created by meikai on 2020/06/02.
 */
public class ResourceUtils {

    private static Resources res = KidApplication.getInstance().getResources();

    public static Resources getResource() {
        return res;
    }

    public static int getColorResource(@ColorRes int resource) {
        return ContextCompat.getColor(KidApplication.getInstance(), resource);
    }

    public static float getDimension(int dimension) {
        return res.getDimension(dimension);
    }

    public static Drawable getDrawable(int resource) {
        Drawable drawable = null;
        try {
            drawable = ContextCompat.getDrawable(KidApplication.getInstance(), resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawable;
    }

    public static InputStream openRawResource(int resource) {
        return res.openRawResource(resource);
    }

    public static String getString(int resId) {
        return res.getString(resId);
    }

    public static String getString(int resId, Object... args) {
        return res.getString(resId, args);
    }

    public static String[] getStringArray(int resource) {
        return res.getStringArray(resource);
    }

    public static byte[] getByteFromRaw(@RawRes int id) {
        byte[] buffer = null;
        InputStream in = null;
        try {
            in = KidApplication.getInstance().getResources().openRawResource(id);
            int length = in.available();
            //创建byte数组
            buffer = new byte[length];
            in.read(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.close(in);
        }
        return buffer;
    }

}
